// import { dictValueTypes } from "@/constants/types";
import { commonSelectType, userTypes } from "@/constants/types";
import { getStorage } from "./storage";
import dayjs from "dayjs";

export const isEmptyObj = (obj: any) => {
  if (!obj || typeof obj !== 'object') {
    return true;
  }
  return !Object.keys(obj).length;
}
// calculate table width
export const calculateTableWidth = (col: Array<any>, defaultWidth: number = 120) => {
  if (!col || !col.length){
    return null;
  }
  return col.reduce((p, v) => {
    if (v.hideInTable) {
      return p;
    }
    return p + (v?.width || defaultWidth);
  }, 0);
}
export const getDefaultPathName = () => {
  return '/dashboard';
};
// 获取用户信息
export const getUser = () => {
  const userInfo = localStorage.getItem('userInfo');
  if (!userInfo) return;
  const currentUser = JSON.parse(userInfo) || {};
  // eslint-disable-next-line consistent-return
  return currentUser;
};
/**
 * 
 * @returns 校验是否登录
 */
export const verifyLogin = () => {
  const token = getStorage('token');
  // const userInfo = getStorage('userInfo');
  // return token && userInfo;
  return !!token;
}
/**
 * 根据key获取label
 * @param {*} list 
 * @param {*} key 
 * @returns 
 */
export const getLabelByKey = (obj: commonSelectType) => {
  const { list, key, dataLabel = 'dictLabel', dataValue = 'dictValue' } = obj;
  if (!Array.isArray(key)) {
    return list?.find((v: any) => v[dataValue] === key)?.[dataLabel] || '';
  }
  if (!key.length) {
    return [];
  }
  return list?.filter((v: any) => key.includes(v[dataValue]))?.map(v => v?.[dataLabel]) || [];
}
// 转换数组成对象
export const arrayConvertToObj = (list: Array<{[x: string]: any;}>, dataValue: string = 'dictValue', dataLabel: string = 'dictLabel') => {
  if(!list || !list.length) {
    return {}
  }
  return list.reduce((p, v) => {
    return {
      ...p,
      [v?.[dataValue]]: [v?.[dataLabel]]
    }
  }, {})
}
// 字典转换成select
export const dictConvertToSelect = (list: Array<{dictValue: string | number, dictLabel: string}>) => {
  if (!list?.length){
    return [];
  }
  return list.map(v => ({label: v?.dictLabel, value: v?.dictValue}))
}
/**
 * 对比两个数组是否相同
 * @param arr1 数组1
 * @param arr2 数组2
 * @param key 对比的key，对象数组key必传
 * @returns 返回值 boolean
 */
export const compareArr = (arr1: any [], arr2: any [], key?: string) => {
  if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
    return false;
  }
  if (arr1?.length !== arr2?.length) {
    return false;
  }
  if (key) {
    return arr1.every(v => arr2.some(s => s[key] === v[key]));
  }
  return arr1.every(v => arr2.some(s => s === v));
}

export const downloadFile = (path: string) => {
  const fileName = path.substring(path.lastIndexOf('/') + 1);
  const link = document.createElement('a');
  link.href = path;
  link.target = "_blank";
  link.download = fileName;
  link.target = "_blank";
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}
// 获取最近七天，共八天
export const getNearlySevenDays = () => {
	const dates = []
	for (let i = 0; i < 8; i++) {
		const date = dayjs().add(i, 'day')
		dates.push(date.format('YYYY-MM-DD'))
	}
	return dates;
}
// 是否为销售
export const userIsSales = (userInfo: userTypes) => {
  return userInfo?.deptKey === 'sales';
}
/**
 * 检查文件上传大小是否超出
 * 默认大小为1M
 */
export const checkFileSize = (target: any, size = 1) => {
  const max_size_byte = size * 1000 * 1000;
  const { size: target_size } = target;
  return max_size_byte >= target_size;
};
export const getFileNameByPath = (path: string) => {
  if (!path || typeof path !== 'string') {
    return '';
  }
  return path.substring(path.lastIndexOf('/') + 1);
};